﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
using MavenThought.Commons.WPF.Events;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using TFS.Kanban.Domain;
using TFS.Kanban.Domain.Events;
using TFS.Kanban.Domain.Services;

namespace TFS.Kanban.Modules.ProjectExplorer.ViewModels
{
    public class ProjectExplorerViewModel : NotificationObject
    {
        private readonly IProjectService _projectService;
        private readonly IIterationService _iterationService;
        private readonly IUserStoryService _userStoryService;
        private readonly IEventAggregator _eventAggregator;

        public ProjectExplorerViewModel(IProjectService projectService,
                                        IIterationService iterationService,
                                        IUserStoryService userStoryService,
                                        IEventAggregator eventAggregator)
        {
            _projectService = projectService;
            _iterationService = iterationService;
            _userStoryService = userStoryService;
            _eventAggregator = eventAggregator;

            this.Iterations = new ObservableCollection<IterationViewModel>();
            this.UserStories = new ObservableCollection<UserStoryViewModel>();

            var view = CollectionViewSource.GetDefaultView(this.Iterations);
            view.CurrentChanged += test;

            Initialize();
        }

        private void test(object sender, EventArgs e)
        {
            
        }

        private void Initialize()
        {
            RegisterCommands();
        }

        private void RegisterCommands()
        {
            this.GetProjectsCommand = new DelegateCommand(() =>
                                                              {
                                                                  this.Projects = new ObservableCollection<IProject>(_projectService.GetProjects());
                                                                  RaisePropertyChanged(() => Projects);
                                                              });

            this.ProjectSelectedCommand = new DelegateCommand(() =>
                                              {
                                                  _eventAggregator.Raise<IProjectSelected>(x => x.Project = this.SelectedProject);

                                                  this.Iterations.Clear();
                                                  foreach (var iteration in _iterationService.GetIterations(this.SelectedProject))
                                                  {
                                                      this.Iterations.Add(new IterationViewModel(iteration));
                                                  }

                                                  //this.UserStories.Clear();
                                                  //foreach (var userStory in _userStoryService.GetUserStories(this.SelectedProject))
                                                  //{
                                                  //    this.UserStories.Add(new UserStoryViewModel(userStory));
                                                  //}
                                              });

            this.PathSelectedCommand = new DelegateCommand(PathSelected);
        }

        private void PathSelected()
        {
            if(this.SelectedPath is IterationViewModel)
            {
                _eventAggregator.Raise<IPathSelected>(x => x.Path = ((IterationViewModel)this.SelectedPath).Iteration.Path);
            }
            else if(this.SelectedPath is IArea)
            {
                _eventAggregator.Raise<IPathSelected>(x => x.Path = ((IArea)this.SelectedPath).Path);
            }
        }

        private IProject _selectedProject;
        public IProject SelectedProject
        {
            get { return _selectedProject; }
            set
            {
                _selectedProject = value;
                RaisePropertyChanged(() => SelectedProject);
            }
        }

        private object _selectedPath;
        public object SelectedPath
        {
            get { return _selectedPath; }
            set
            {
                _selectedPath = value;
                RaisePropertyChanged(() => SelectedPath);
            }
        }

        public ObservableCollection<IProject> Projects { get; private set; }

        public ObservableCollection<IterationViewModel> Iterations { get; set; }
        public ObservableCollection<UserStoryViewModel> UserStories { get; set; }

        public ICommand GetProjectsCommand { get; private set; }
        public ICommand ProjectSelectedCommand { get; private set; }
        public ICommand PathSelectedCommand { get; private set; }
    }
}